// packageD/member/myRelationship/amount/amount.js
var App = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    isTotal: false, //* 是否是总客户数
    showLevelId: "", //* 当前显示的level id
    showLevelElId: "levelEl", //* 当前显示的 元素 id
    showLevelName: "level_", //* 当前显示的levelName id
    levels: [], //* 所有等级 用于tabbar
    memberOfLevel: {}, //* 等级下的会员
    totalMemberCount: 0, //* 全部会员数量 用于tabbar 第一个
    levelPagination: {}, //* 分页记录
    defaultLevelName: "", //* 如果没有level名称就显示这个默认的
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const agentLanguage = JSON.parse(wx.getStorageSync("mailLanguage")).agent;
    let title = agentLanguage.agent_num || "客户数量";
    if (options.type) {
      title = agentLanguage.agent_count || "总客户数量";
      this.setData({
        isTotal: true,
      });
    }
    wx.setNavigationBarTitle({
      title,
    });
    this.getLevelMembers();
  },
  onReachBottom() {
    this.getLevelMembers();
  },
  switchLevelTab(event) {
    const dataset = event.currentTarget.dataset;
    this.setData(
      {
        showLevelId: dataset.id,
        showLevelElId: "levelEl" + dataset.id,
        showLevelName: "level_" + dataset.id,
      },
      () => {
        if (this.data.memberOfLevel["level_" + dataset.id].length === 0) {
          this.getLevelMembers();
        }
      }
    );
  },
  getLevelMembers() {
    wx.showLoading({
      title: "加载中",
    });
    const showLevelId = this.data.showLevelId;
    const levelName = "level_" + showLevelId;
    let currentPage = this.data.levelPagination[levelName];
    let url = null;
    if (this.data.isTotal) {
      url = App.getNetAddresss(
        "plugin.member-center-agent.api.index.my-agent-all"
      );
    } else {
      url = App.getNetAddresss("plugin.member-center-agent.api.index.my-agent");
    }

    if (showLevelId!=="") {
      url += "&level_id=" + showLevelId;
    }

    if (currentPage) {
      url += "&page=" + currentPage["page"];
      if (currentPage["finished"] === true || currentPage["loading"] === true) {
        wx.hideLoading();
        return;
      }
      this.setData({
        ["levelPagination." + levelName + ".loading"]: true,
      });
    }
    App._getNetWork({
      url,
      success: ({ data: { result, data, msg } }) => {
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
            duration: 1500,
            mask: false,
            success: (result) => {
              setTimeout(() => {
                wx.redirectTo({
                  url: "/packageD/member/myRelationship/myRelationship",
                });
              }, 1500);
            },
          });
        }
        const setData = {};
        if (this.data.levels.length === 0) {
          setData["levels"] = data.level;
          setData["memberOfLevel"] = {
            level_0: [],
          };
          setData["levelPagination"] = {};
          let totalMemberCount = 0;
          for (const levelItem of data.level) {
            setData["memberOfLevel"]["level_" + levelItem.id] = [];
            setData["levelPagination"]["level_" + levelItem.id] = {
              loading: false,
              finished: false,
              page: 1,
            };
            totalMemberCount += levelItem.yz_member_count;
          }
          setData["levelPagination"]["level_"] = {
            loading: false,
            finished: false,
            page: 2,
          };
          setData["memberOfLevel"][levelName] = data.child.data;
          setData["totalMemberCount"] = totalMemberCount;
          setData["defaultLevelName"] = data.default_level_name;
        } else {
          let localLevelData = this.data.memberOfLevel[levelName];
          localLevelData.push(...data.child.data);
          setData["memberOfLevel." + levelName] = localLevelData;
          setData["levelPagination." + levelName + ".page"] =
            currentPage["page"] + 1;
        }
        if (
          data.child.data.length < data.child.per_page ||
          data.child.data.length === 0
        ) {
          setData["levelPagination." + levelName + ".finished"] = true;
        }
        setData["levelPagination." + levelName + ".loading"] = false;

        this.setData(setData);
        wx.hideLoading();
      },
    });
  },
});
